<template>
  <div class="home">
    <header>
      <h1>vue3.0 & echarts</h1>
    </header>
    <section class="container overflow-hidden">
      <section class="item-left">
        <ItemPage>
          <BarChart title="销售总量" v-if="barData && barData.length" :chartData="barData" :height="'4.5rem'" /> 
        </ItemPage>
        <ItemPage>
          <LineChart title="产品周销" :height="'4.5rem'" />
        </ItemPage>
      </section>
      <section class="item-center">
        <MapPage :title="'城市销售量'" />
      </section>
      <section class="item-right">
        <ItemPage>
          <PieChart title="库存统计" :height="'4.5rem'" />
        </ItemPage>
        <ItemPage>
          <BarStackChart title="产品类别" :height="'4.5rem'" />
        </ItemPage>
      </section>
    </section>
  </div>
</template>

<script>
// @ is an alias to /src
import ItemPage from '@/components/ItemPage'
import BarChart from '@/components/BarChart'
import LineChart from '@/components/LineChart'
import PieChart from '@/components/PieChart'
import MapPage from '@/components/MapPage'
import BarStackChart from '@/components/BarStackChart'

import { inject, reactive, onMounted, toRefs } from 'vue'

export default {
  name: 'Home',
  components: {
    BarChart, LineChart, PieChart, ItemPage, MapPage, BarStackChart
  },
  setup() {
    let state = reactive({
      barData:[]
    })
    let $http = inject('axios');

    function getState() {
      return new Promise((resolve, reject) => {
        $http({url: '/bar/data'}).then((res) => {
          resolve(res)
        })
      })
    }

    onMounted(async () => {
      let barD = await getState()
      state.barData = [...barD.data.data.chartData]
    })
    
    return {
      ...toRefs(state)
    }

  }
}
</script>

<style lang="less">
header {
  width: 100%;
  height: .8rem;
  background-color: rgba(255, 255, 255, .2);
  h1 {
    line-height: .8rem;
    font-size: .375rem;
    text-align: center;
  }
}

.container {
  // 最大最小的宽度
  // min-width: 1200px;
  // max-width: 2048px;
  // margin: 0 auto;

  padding: .25rem;
  display: flex;
  .item-left {
    flex: 3;
    display: flex;
    flex-direction: column;
  }
  .item-center {
    flex: 5;
    height: 11rem;
    border: 1px solid blue;
    padding: .125rem;
    margin: .25rem;

    display: flex;
    flex-direction: column;
  }
  .item-right {
    flex: 3;
    display: flex;
    flex-direction: column;
  }

  h2 {
    height: .6rem;
    line-height: .7rem;
    font-size: .25rem;
    color: #fff;
    text-align: center;
  }
}

</style>
